<%--
  Created by IntelliJ IDEA.
  User: Jafrin
  Date: 9/10/12
  Time: 8:13 PM
  To change this template use File | Settings | File Templates.
--%>



<script type="text/javascript" language="Javascript">
    $(document).ready(function() {
//       $('#ui-widget-header-text').html('Interview')
//      textboxes = $("input:visible:not([disabled],[readonly]),select,button");
//    if ($.browser.mozilla) {
//        $(textboxes).keypress (checkForEnter);
//    } else {
//        $(textboxes).keydown (checkForEnter);
//    }
//
//    $("#gFormInterview").validationEngine({   //    client side validation
//      isOverflown: true,
//      overflownDIV: ".ui-layout-center"
//    });
//    $("#gFormInterview").validationEngine('attach');
        reset_form("#gFormInterview");
        %{--$("#jqgrid-grid").jqGrid({--}%
        %{--url:'${resource(dir:'interviewInstance', file:'list')}',--}%
        %{--datatype: "json",--}%
        %{--colNames:[--}%
        %{--'SL',--}%
        %{--'ID',--}%
        %{----}%
        %{--'Interviewer',--}%
        %{--'Question',--}%
        %{--'Question Answering Option'--}%
        %{--],--}%
        %{--colModel:[--}%
        %{--{name:'sl',index:'sl', width:30, sortable:false, align:'left'},--}%
        %{--{name:'id',index:'id', width:0, hidden:true},--}%
        %{----}%
        %{--{name:'interviewer', index:'interviewer',width:100,align:'left'},--}%
        %{--{name:'question', index:'question',width:100,align:'left'},--}%
        %{--{name:'questionAnsweringOption', index:'questionAnsweringOption',width:100,align:'left'}--}%
        %{--],--}%
        %{--rowNum:50,--}%
        %{--rowList:[10,20,30,40,50,60,70,80,90,100,-1],--}%
        %{--pager: '#jqgrid-pager',--}%
        %{--sortname: 'id',--}%
        %{--viewrecords: true,--}%
        %{--sortorder: "desc",--}%
        %{--caption:"All Interview Information",--}%
        %{--autowidth: true,--}%
        %{--height: 350,--}%
        %{--scrollOffset: 0,--}%
        %{--loadComplete: function() {--}%
        %{--},--}%
        %{--onSelectRow: function(rowid, status) {--}%
        %{--executeEditInterview();--}%
        %{--}--}%
        %{--});--}%
        %{--$("#jqgrid-grid").jqGrid('navGrid', '#jqgrid-pager', {edit:false,add:false,del:false,search:false});--}%
        %{--$("#jqgrid-grid").gridResize({minWidth:350,maxWidth:800,minHeight:200});--}%
        %{--$(".ui-pg-selbox").children().each(function() {--}%
        %{--if ($(this).val() == -1) {--}%
        %{--$(this).html('All')--}%
        %{--}--}%

        %{--});--}%

        $("#flex-grid-table").flexigrid({

            url: "${resource(dir:'interview', file:'list')}",
            dataType: 'json',
            colModel : [
                {display: 'SL', name : 'sl', width : 150, sortable : true, align: 'center',process:executeEditInterview},
                {display: 'ID', name : 'id', width : 180, sortable : true, align: 'left',hide: true,process:executeEditInterview},
                {display: 'question', name : 'question', width : 150, sortable : true, align: 'center' ,process:executeEditInterview},
                {display: 'questionAnsweringOption', name : 'questionAnsweringOption', width : 180, sortable : true, align: 'left',process:executeEditInterview}
            ],
            buttons : [
//              {name: 'Add', bclass: 'add', onpress : test},
//              {name: 'Delete', bclass: 'delete', onpress : test},
//              {separator: true}
            ],

            searchitems : [
                {display: 'Code', name : 'code'},
                {display: 'Name', name : 'name', isdefault: true}
            ],
            singleSelect: true,
//          singleSelect:function(rowid)
//          {
//            alert(rowid)
//          },
            sortname: "name",
            sortorder: "asc",
            usepager: true,
            title: 'Project Information',
            useRp: true,
            rp: 15,
            showTableToggleBtn: true,
            width: 800,
            height: 200,
            singleSelect:true
        });

    });
    function getSelectedInterviewId()
    {
        var interviewInstanceId = null;
        var rowid = $("#jqgrid-grid").jqGrid('getGridParam', 'selrow');
        if(rowid)
        {
            interviewInstanceId = $("#jqgrid-grid").jqGrid('getCell', rowid, 'id');
        }
        if(interviewInstanceId == null){
            interviewInstanceId = $('#gFormInterview input[name = id]').val();
        }
        return interviewInstanceId;
    }
    function executePreConditionInterview() {
        // trim field vales before process.
        trim_form();
        if ($("#gFormInterview").validate().form({onfocusout: false}) == false) {
            return false;
        }
        return true;
    }
    function executeAjaxInterview() {
        if(!executePreConditionInterview()) {
            return false;
        }
        var actionUrl = null;
        if ($('#gFormInterview input[name = id]').val()) {
            actionUrl = "${request.contextPath}/${params.controller}/update";
        } else {
            actionUrl = "${request.contextPath}/${params.controller}/create";
        }
        jQuery.ajax({
            type:'post',
            data:jQuery("#gFormInterview").serialize(),
            url: actionUrl,
            success:function(data, textStatus) {
                executePostConditionInterview(data);
            },
            error:function(XMLHttpRequest, textStatus, errorThrown) {
            },
            complete:function(){
            },
            dataType:'json'
        });
        return false;
    }
    function executePostConditionInterview(result) {
        if (result.type == 1) {
            $("#jqgrid-grid").trigger("reloadGrid");
            reset_form('#gFormInterview');
        }
        MessageRenderer.render(result);
    }
    function deleteAjaxInterview() {    // Delete record
        var interviewInstanceId = getSelectedInterviewId();
        if(executePreConditionForDeleteInterview(interviewInstanceId))
        {
            $("#dialog").dialog("destroy");
            $("#dialog-confirm-interviewInstance").dialog({
                resizable: false,
                height:150,
                modal: true,
                title: 'Delete alert',
                buttons: {
                    'Delete item(s)': function() {
                        $(this).dialog('close');
                        $.ajax({
                            type: "POST",
                            dataType: "json",
                            data:jQuery("#gFormInterview").serialize(),
                            url: "${resource(dir:'interviewInstance', file:'delete')}",
                            success: function(message) {
                                executePostConditionForDeleteInterview(message);
                            }
                        });
                    },
                    Cancel: function() {
                        $(this).dialog('close');
                    }
                }
            }); //end of dialog
        }
    }

    function executePreConditionForEditInterview(interviewInstanceId) {
        if(interviewInstanceId == null)
        {
            alert("Please select a interviewInstance to edit") ;
            return false;
        }
        return true;
    }
    function executeEditInterview() {
    %{--var interviewInstanceId = getSelectedInterviewId();--}%
    %{--if(executePreConditionForEditInterview(interviewInstanceId))--}%
    %{--{--}%
    %{--$.ajax({--}%
    %{--type: "POST",--}%
    %{--url: "${resource(dir:'interviewInstance', file:'edit')}?id="+ interviewInstanceId,--}%
    %{--success: function(entity) {--}%
    %{--executePostConditionForEditInterview(entity);--}%
    %{--},--}%
    %{--dataType:'json'--}%
    %{--});--}%
    %{--}--}%
    }
    function executePostConditionForEditInterview(data) {
        if (data == null) {
            alert('Selected interviewInstance might have been deleted by someone');  //Message renderer
        } else {
            showInterview(data);
        }
    }
    function executePreConditionForDeleteInterview(interviewInstanceId) {
        if(interviewInstanceId == null)
        {
            alert("Please select a interviewInstance to delete") ;
            return false;
        }
        return true;
    }
    function executePostConditionForDeleteInterview(data) {
        if (data.type ==1) {
            $("#jqgrid-grid").trigger("reloadGrid");
            reset_form('#gFormInterview');
        }
        MessageRenderer.render(data)
    }
    function showInterview(entity) {
        $('#gFormInterview input[name = id]').val(entity.id);
        $('#gFormInterview input[name = version]').val(entity.version);

        if(entity.interviewer){
            $('#interviewer').val(entity.interviewer.id).attr("selected","selected");
        }
        else{
            $('#interviewer').val(entity.interviewer);
        }
        if(entity.question){
            $('#question').val(entity.question.id).attr("selected","selected");
        }
        else{
            $('#question').val(entity.question);
        }
        if(entity.questionAnsweringOption){
            $('#questionAnsweringOption').val(entity.questionAnsweringOption.id).attr("selected","selected");
        }
        else{
            $('#questionAnsweringOption').val(entity.questionAnsweringOption);
        }
        $('#create-button').attr('value', 'Update');
        $('#cancel-button').attr('value', 'Cancel');
        $('#delete-button').show();
    }
    function executeSearchInterview(fieldName, fieldValue){
        if(executePreConditionForSearchInterview(fieldName, fieldValue))
        {
            $.ajax({
                type: "POST",
                url: "${resource(dir:'interviewInstance', file:'search')}?fieldName="+fieldName+"&fieldValue="+fieldValue,
                success: function(entity) {
                    executePostConditionForSearchInterview(entity, fieldName, fieldValue);
                },
                dataType:'json'
            });
        }
    }
    function executePreConditionForSearchInterview(fieldName,fieldValue) {
        // trim field vales before process.
        $('#'+fieldName).val($.trim($('#'+fieldName).val()));
        if(fieldValue == '' || fieldValue == null){
            reset_form("#gFormInterview");
            return false;
        }
        return true;
    }
    function executePostConditionForSearchInterview(data, fieldName, fieldValue) {
        if (data == null) {
            reset_form("#gFormInterview"); // Clear Form
            $('#'+fieldName).val(fieldValue); // Set search field with fieldValue
        } else {
            showInterview(data);
        }
    }
</script>